package com.og.ouroa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.og.ouroa.common.exception.Assert;
import com.og.ouroa.common.result.ResponseEnum;
import com.og.ouroa.pojo.entity.Course;
import com.og.ouroa.pojo.entity.CourseStudent;
import com.og.ouroa.mapper.CourseStudentMapper;
import com.og.ouroa.pojo.entity.UserInfo;
import com.og.ouroa.pojo.vo.GradeTopVO;
import com.og.ouroa.service.CourseStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Us
 * @since 2021-12-13
 */
@Service
public class CourseStudentServiceImpl extends ServiceImpl<CourseStudentMapper, CourseStudent> implements CourseStudentService  {


    @Override
    public IPage<Course> getBySid(Page<Course> page, Integer sid) {
      return baseMapper.getBySid(page,sid);
    }

    @Override
    public IPage<UserInfo> getByCid(Page<UserInfo> page, Integer cid) {
        return baseMapper.getByCid(page,cid);
    }

    @Override
    public CourseStudent getGrade(Integer sid, Integer cid) {
        QueryWrapper<CourseStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("cid",cid)
        .eq("sid",sid);


        return baseMapper.selectOne(wrapper);
    }

    @Override
    public Integer countPass(Integer cid) {
        QueryWrapper<CourseStudent> wrapper = new QueryWrapper<>();
        wrapper.ge("grade",60);
        wrapper.eq("cid",cid);
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public Integer countSelection(Integer cid) {
        QueryWrapper<CourseStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("cid",cid);
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public IPage<GradeTopVO> ListByCidTop(Page<GradeTopVO> page, Integer cid, Integer TYPE,Double grade) {
        return baseMapper.greadTopByCid(page,cid,TYPE,grade);

    }



    @Override
    public void insert(Integer sid, Integer cid) {

        //断言为空
        QueryWrapper<CourseStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("cid",cid).eq("sid",sid);
        CourseStudent res = baseMapper.selectOne(wrapper);
        Assert.isNull(res, ResponseEnum.REPEAT_SELECTION);

      baseMapper.insertOne(sid,cid);

    }

    @Override
    public void insertGrade(Integer sid, Integer cid, Double grade) {
        QueryWrapper<CourseStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("cid",cid).eq("sid",sid);
        CourseStudent res = baseMapper.selectOne(wrapper);
        //断言数据不为空
        Assert.notNull(res, ResponseEnum.REPEAT_SELECTION);

        res.setGrade(grade);

        baseMapper.update(res,wrapper);


    }

    @Override
    public void delete(Integer sid, Integer cid) {
        QueryWrapper<CourseStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("cid",cid).eq("sid",sid);
        CourseStudent res = baseMapper.selectOne(wrapper);
        //断言数据不为空
        Assert.notNull(res, ResponseEnum.GRAND_NULL);
        baseMapper.delete(wrapper);
    }

    @Override
    public IPage<CourseStudent> ListBySid(Page<CourseStudent> page, Integer sid) {
        QueryWrapper<CourseStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("sid",sid);
         return baseMapper.selectPage(page, wrapper);
    }


}
